package com.xinqi.modules.prepare.prepare.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.prepare.common.constant.PrepareConstant;
import com.xinqi.modules.prepare.prepare.domain.PrepareEntity;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareQueryDTO;
import com.xinqi.modules.prepare.prepare.dto.rsp.PrepareResultDTO;
import com.xinqi.modules.prepare.prepare.manager.PrepareManager;
import com.xinqi.modules.prepare.prepare.mapper.PrepareMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.xinqi.common.base.error.CommonErrors.BAD_REQUEST;

/**
 * 备课区管理层实现
 *
 * @author: system
 * @date: 2023/03/19
 */
@Service
@RequiredArgsConstructor
public class PrepareManagerImpl extends BaseServiceImpl<PrepareMapper, PrepareEntity> implements PrepareManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<PrepareResultDTO> findPage(Page<Object> page, PrepareQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<PrepareResultDTO> findList(PrepareQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 备课区数据
     */
    @Override
    public PrepareEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 备课区数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    /**
     * 校验当前用户的备课区名称是否已存在
     */
    @Override
    public boolean isExistName(Long userId, String name, Long oldId) {
        LambdaQueryWrapper<PrepareEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareEntity::getUserId, userId);
        wrapper.eq(PrepareEntity::getName, name);
        wrapper.eq(PrepareEntity::getIsDelete, YesNoEnum.NO.ordinal());
        wrapper.ne(oldId != null, PrepareEntity::getId, oldId);
        return count(wrapper) > 0;
    }

    @Override
    public boolean isExistDefault(Long userId) {
        LambdaQueryWrapper<PrepareEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareEntity::getUserId, userId);
        wrapper.eq(PrepareEntity::getIsDefault, YesNoEnum.YES.getCode());
        wrapper.eq(PrepareEntity::getIsDelete, YesNoEnum.NO.ordinal());
        return count(wrapper) > 0;
    }

    @Override
    public Long insertDefault(Long userId) {
        BAD_REQUEST.check(userId != null, "用户Id不能为空");
        BAD_REQUEST.check(!isExistDefault(userId), "默认备课区不能重复创建");
        PrepareEntity entity = new PrepareEntity();
        entity.setUserId(userId);
        entity.setName(PrepareConstant.PREPARE_DEFAULT_NAME);
        entity.setIsDefault(YesNoEnum.YES.ordinal());
        entity.setIsEdit(YesNoEnum.YES.ordinal());
        save(entity);
        return entity.getId();
    }

    @Override
    public Long prepareCount(Long userId) {
        return prepareCount(userId, null);
    }

    @Override
    public Long prepareCount(Long userId, Boolean isDefault) {
        LambdaQueryWrapper<PrepareEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareEntity::getUserId, userId);
        if (isDefault != null) {
            wrapper.eq(PrepareEntity::getIsDefault, isDefault ? YesNoEnum.YES.ordinal() : YesNoEnum.NO.ordinal());
        }
        wrapper.eq(PrepareEntity::getIsDelete, YesNoEnum.NO.ordinal());
        return count(wrapper);
    }

}
